<?php

namespace app\common\library;

use Intervention\Image\ImageManager;
/**
 * http://image.intervention.io/getting_started/installation
 *  图像处理聚合类
 */
class Img
{
    public $manager;
    public function __construct($driver = 'gd')
    {
        $this->manager = new ImageManager(array('driver' => $driver));
    }

    //获取base64图片->encode('png', 50)
    public function getBase64($path) {
        $image = $this->manager->make('.'.$path);
        $height = $image->height();
        $width = $image->width();
        $radio = $width/$height;
        if ($width > $height) {
            if ($width > 500) {
                $width = 500;
                $height = intval(500/$radio);
            }
        } else {
            if ($height > 500) {
                $height = 500;
                $width = intval(500*$radio);
            }
        }

        $image->resize($width, $height)->encode('png', 50);
        $base64_encode = base64_encode($image);
        return $base64_encode;
    }

}
